-- summon_attrib
-- create by shuzy
-- 道具：  增加宠物属性

return {
    apply = function(source, target, classId, value)
        local attrib = value[2];

        local summonInfo = source.dbase:query("summon");
        -- 当前没有召唤兽
        if not summonInfo then
            return;
        end

        for pos, data in pairs(summonInfo) do
            -- 如果是目标召唤兽
            local summonId = data["class_id"];
            local grid = DungeonM.getGridByPos(pos);
            local summon = grid.summon;
            if summonId == value[1] and summon and not summon:isDead() then
                if "hp" == attrib then
                    local maxHp = SummonM.query(value[1], "max_hp");
                    local curHp = summon:queryAttrib("hp");
                    local addHp = math.modf(maxHp * value[3] / 1000);

                    if curHp + addHp < maxHp then
                        summon:addAttrib("hp", addHp);
                    elseif curHp < maxHp then
                        summon:addAttrib("hp", maxHp - curHp);
                    end
                elseif "attack" == attrib then
                    local addAttack = math.modf(summon:queryAttrib("attack") * value[3] / 1000);
                    summon:addAttrib("attack", addAttack);
                end
            end

            EventMgr.fire(event.SUMMON_RECOVER, pos);
        end

        return value[3];
    end,
};
